package com.kexun.service.impl;

import com.kexun.User;
import com.kexun.service.SSOService;
import com.kexun.utils.Msg;
import com.kexun.utils.RsaUtil;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@RestController
public class SSOServiceImpl implements SSOService {
    @Override
    public Msg getPublicKey(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        Msg msg = new Msg().success(RsaUtil.getPublicKey());
        return msg;
    }

    @Override
    public Msg login(User user, HttpSession session, HttpServletResponse response, HttpServletRequest request) {
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Headers", "content-type,authorization,x-requested-with");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
        if (user != null) {
            //解密
            String password = RsaUtil.jiemi(user.getPassword());
            System.out.println(password);
            session.setAttribute("user", user);
        } else {

            return new Msg().error("缺少参数");
        }


        return new Msg().success("登录成功>>>> " + session.getId());
    }

    @Override
    public Msg loginOut() {
        return null;
    }


}
